package com.wbp.cms.qatest.service;

import javax.annotation.PostConstruct;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

//@Service("aService")
public class AServiceImpl implements AService ,ApplicationContextAware ,InitializingBean{

	 private ApplicationContext context; 
	 private AService proxy ;
	
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void test1() {
      System.out.println("----test1-------:"+this.getClass().hashCode());
      proxy.test2();
	}

	@Transactional(propagation=Propagation.REQUIRES_NEW)
	@Override
	public void test2() {
		System.out.println("----test2-------:"+this.getClass().hashCode());
		
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		 this.context = applicationContext ;
		 System.out.println("----------setApplicationContext---------");
	}
	
	 @PostConstruct  //③ 初始化方法  
	    private void setSelf() {  
	        //从上下文获取代理对象（如果通过proxtSelf=this是不对的，this是目标对象）  
	        //此种方法不适合于prototype Bean，因为每次getBean返回一个新的Bean  
	        proxy = context.getBean(AService.class);   
	        System.out.println("--------setSelf------:"+proxy);
	    }

	@Override
	public void afterPropertiesSet() throws Exception {
		 System.out.println("----AServiceImpl-------afterPropertiesSet-------");
		
	}  

}
